package de.backtomedieval.game;

import java.util.Random;

import de.backtomedieval.units.UnitGroup;
import de.backtomedieval.units.UnitSet;

public class Balancing {
	
	static final int roundCount = 20;
	
	public static void testFight(UnitSet a, UnitSet b) {
		System.out.println("***************************");
		UnitGroup m = a.makeUnitGroup();
		UnitGroup e1 = b.makeUnitGroup();
		System.out.println("TESTFIGHT:");
		System.out.println(a.toString());
		System.out.println(" ^(ATT) versus (DEF)v");
		System.out.println(b.toString());
		System.out.println("");
		for(int i=0;i<roundCount;i++) {
			m.attackRound(e1, i+1,null);
		}
		System.out.println("***************************");
	}
	
	public static void superBalancing(UnitSet balance) {
		System.out.println("***************************");
		System.out.println("Search Balancing for Attacking Group: " + balance.toString() + " -> sum: " + balance.getSum());
		balancingCheck(balance,UnitSet.unitTypesOnlyOne[0],1);
		System.out.println("");
		balancingCheck(balance,UnitSet.unitTypesOnlyOne[1],1);
		System.out.println("");
		balancingCheck(balance,UnitSet.unitTypesOnlyOne[2],1);
		System.out.println("");
		balancingCheck(balance,UnitSet.unitTypesOnlyOne[3],1);
		System.out.println("");
		balancingCheck(balance,UnitSet.unitTypesOnlyOne[4],1);
		System.out.println("");
		balancingCheck(balance,UnitSet.unitTypesOnlyOne[5],1);
		System.out.println("");
		balancingCheck(balance,UnitSet.unitTypesAll,10);
		System.out.println("***************************");
		System.out.println("");
	}
	
	
	public static void balancingCheck(UnitSet balance, boolean[] useUnits, int resultAnz) {
		
		System.out.println("Try to balance with: " + UnitSet.unitTypesToString(useUnits));
		int max = (balance.getSum());
		UnitSet b = new UnitSet(null);
		Random ra = new Random();
		int diff;
		int anz = 0;
		int tests = 0;
		int diff2;
		
		b.increaseRandom(max, useUnits);
		
		
		do {
			tests++;
			diff = checkBalance(balance, b);
			diff2 = diff;
			if (diff2 < 0) {diff2=diff2 * (-1);}
			if (diff2 < 30) {
				System.out.println("  Found: " + b.toString() + " -> sum: " + b.getSum());
				anz++;
				b.setRandom(max, UnitSet.unitTypesAll);
			} else {
				if (diff < -400) {
					b.decreaseRandom((int) (max * 0.5)+2, useUnits);
				} else if (diff < -200) {
					b.decreaseRandom((int) (max * 0.1)+2, useUnits);
				} else if (diff < -30) {
					b.decreaseRandom(2, useUnits);
				}
				if (diff > 400) {
					b.increaseRandom((int) (max * 0.5)+2, useUnits);
				} else if (diff > 200) {
					b.increaseRandom((int) (max * 0.1)+2, useUnits);
				} else if (diff > 30) {
					b.increaseRandom(2, useUnits);
				}
			}
		}while(anz < resultAnz && tests < 500);
		if (anz == 0) {
			System.out.println("  No Result for Balancing!");
		}
	}

	
	public static int checkBalance(UnitSet attacker, UnitSet defencer) {

		int aWins = 0;
		int bWins = 0;
		
		for (int i =0; i<500; i++) {
			UnitGroup a = attacker.makeUnitGroup();
			a.silent=true;
			UnitGroup b = defencer.makeUnitGroup();
			b.silent=true;
			for(int j=0;j<roundCount;j++) {
				a.attackRound(b, j+1,null);
			}
			if (a.getUnits().isEmpty()) {
				bWins++;
			}
			if (b.getUnits().isEmpty()) {
				aWins++;
			}
		}
		int diff=aWins - bWins;
		return diff;
		
	}
}
